//
//  VolunteerInputViewController.m
//  MemberProject
//
//  Created by mac on 15/4/27.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "VolunteerInputViewController.h"
#import "ChoiceDateView.h"
@interface VolunteerInputViewController ()
{
    ChoiceDateView *dateView;
    NSString *beginDateStr;
    NSString *endDateStr;
}
@end

@implementation VolunteerInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"活动录入";
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setupView];
}
-(void)setupView
{
    [self.UnitBtn addTarget:self action:@selector(addUnit:) forControlEvents:64];
    
    [self.beginTimeBtn addTarget:self action:@selector(addDate:) forControlEvents:64];
    [self.endTimeBtn addTarget:self action:@selector(addDate:) forControlEvents:64];
}
-(void)addUnit:(UIButton *)sender
{
    [self showQz:MyZbarStyle];
    [self getZBarPushBlockStr:^(id result) {
        [SVProgressHUD showInfoWithStatus:result maskType:SVProgressHUDMaskTypeBlack];
    }];
}
-(void)addDate:(UIButton *)sender
{
    
    __block CSVoidBlock backBlock = ^(void){
        [UIView animateWithDuration:0.5 animations:^{
            dateView.frame = CGRectMake(5, self.view.height, SCREEN_WIDTH-10, 200);
        } completion:^(BOOL finished) {
            [dateView removeFromSuperview];
        }];
    };
    
    
    if (!dateView) {
        dateView=[[[NSBundle mainBundle] loadNibNamed:@"ChoiceDateView" owner:self options:nil]lastObject];
    }
    if (![self.view.subviews containsObject:dateView]) {
        dateView.frame = CGRectMake(5, self.view.height, SCREEN_WIDTH-10, 200);
        [self.view addSubview:dateView];
        
        [UIView beginAnimations:@"choiceDate" context:nil];
        [UIView setAnimationDuration:0.5];
        dateView.frame =CGRectMake(5, self.view.height-200-5, SCREEN_WIDTH-10, 200);
        [UIView commitAnimations];
    }
    if (sender.tag ==21) {
        dateView.infoLabel.text = @"活动开始时间";
    }else
        dateView.infoLabel.text = @"活动结束时间";
    
   
    [dateView doneActionBlock:^(NSString *submitStr) {
        backBlock();
        if (sender.tag ==21) {
            beginDateStr = submitStr;
        }else
            endDateStr = submitStr;
        
        [sender setTitle:submitStr forState:0];
    }];
    [dateView backActionBlock:^{
        backBlock();
    }];
    
    

}
- (IBAction)subAction:(UIButton *)sender
{
    if (!beginDateStr) {
        [SVProgressHUD showErrorWithStatus:@"活动开始时间不能为空" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    if (!endDateStr) {
        [SVProgressHUD showErrorWithStatus:@"活动结束时间不能为空" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日HH时mm分"];
    NSDate *beginDate = [dateFormatter dateFromString:beginDateStr];
    NSDate *endDate = [dateFormatter dateFromString:endDateStr];
    NSTimeInterval timebe = [endDate timeIntervalSinceDate:beginDate];
    if (timebe>0) {
        [SVProgressHUD showSuccessWithStatus:@"成功" maskType:SVProgressHUDMaskTypeBlack];
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"结束时间不得早于开始时间" maskType:SVProgressHUDMaskTypeBlack];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
